package com.colabo.j2ee.web.oaf.monitorperspective.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.colabo.j2ee.web.core.dictionary.CodeInfoCache;
import com.colabo.j2ee.web.core.dictionary.IPCodeInfoVO;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.core.persistence.DateToStringUserType;
import com.colabo.j2ee.web.core.pkgenerator.IPKGenerator;
import com.colabo.j2ee.web.core.service.impl.DefaultServiceImpl;
import com.colabo.j2ee.web.oaf.monitorperspective.ILogDataService;
import com.colabo.j2ee.web.oaf.vo.IPAuditLogVO;

public class LogDataServiceImpl extends DefaultServiceImpl implements ILogDataService {
	// 应用日志数典编码
	private static final String CODE = "BizModule";
	// 应用日志开关状态 1为开启 0为关闭
	private static final String ISLOGOPENSTAT = "0";
	
	public List loadLogData(Map params, int offset, int pageSize) throws ServiceException{
		List l = find_iBatis("logdata.queryLog", params, offset, pageSize);
		if (l != null && !l.isEmpty()) {
			for (int i = 0; i < l.size(); i++) {
				IPAuditLogVO logvo = (IPAuditLogVO) l.get(i);
				String name = logvo.getOpName();
				if (name == null || name.equals("")) {
					logvo.setOpName("未知用户");
				}
			}
		}
		return l;
	}
	
	public int getLogDataCount(Map params) throws ServiceException {
		List l = find_iBatis("logdata.queryLogCount", params);
		if (l != null && !l.isEmpty()) {
			return ((Integer) l.get(0)).intValue();
		}
		return 0;
	}
	
	/**
	 * 记录登录日志信息到日志表
	 * 
	 * @param bizType
	 *            业务模块类型
	 * @param opType
	 *            操作类型
	 * @param opCode
	 *            登录用户代码	
	 * @param ipaddr
	 *            登录用户地址
	 * @param logInfo
	 *            操作信息
	 */
	public void saveLog(String bizType, String opType, String opCode, String ipaddr, String logInfo)
			throws ServiceException {
		
		CodeInfoCache cic = CodeInfoCache.newInstance();
		IPCodeInfoVO vo = cic.getCodeInfoVO(CODE, bizType);
		
		if(vo!=null){	
			if (ISLOGOPENSTAT.equals(vo.getExt1())) {
				return;
			}
		}else{
			return;
		}
		String dateNow = DateToStringUserType.formatDate(new Date(), DateToStringUserType.DATA_TO_STRING_FORMATE);

		IPKGenerator keyGen = (IPKGenerator) getBean(IPKGenerator.BEAN_ID);

		IPAuditLogVO logvo = new IPAuditLogVO();
		logvo.setAuditlogky(keyGen.getUUIDKey(logvo));
		logvo.setBizType(bizType);
		logvo.setOpCode(opCode);
		logvo.setLogType(opType);
		logvo.setIpAddr(ipaddr);
		logvo.setLogTime(dateNow);
		logvo.setLogInfo(logInfo);
		logvo.setLogLevel("");

		save_Hibernate_Object(logvo);
	}

	/**
	 * 记录登录日志信息到日志表
	 * 
	 * @param params  业务模块类型
	 */
	public void saveLog(Map params) throws ServiceException {
		String bizType = params.get("bizType") == null ? null : params.get("bizType").toString().trim();
		String opType = params.get("opType") == null ? null : params.get("opType").toString().trim();
		String opCode = params.get("opCode") == null ? null : params.get("opCode").toString().trim();
		String ipaddr = params.get("ipaddr") == null ? null : params.get("ipaddr").toString().trim();
		String logInfo = params.get("logInfo") == null ? null : params.get("logInfo").toString().trim();
		String logLevel = params.get("logLevel") == null ? null : params.get("logLevel").toString().trim();
		
		CodeInfoCache cic = CodeInfoCache.newInstance();
		IPCodeInfoVO vo = cic.getCodeInfoVO(CODE, bizType);
		
		if(vo!=null){	
			if (ISLOGOPENSTAT.equals(vo.getExt1())) {
				return;
			}
		}else{
			return;
		}		
		
		String dateNow = DateToStringUserType.formatDate(new Date(), DateToStringUserType.DATA_TO_STRING_FORMATE);

		IPKGenerator keyGen = (IPKGenerator) getBean(IPKGenerator.BEAN_ID);
		IPAuditLogVO logvo = new IPAuditLogVO();
		logvo.setAuditlogky(keyGen.getUUIDKey(logvo));
		logvo.setBizType(bizType);
		logvo.setOpCode(opCode);
		logvo.setLogType(opType);
		logvo.setIpAddr(ipaddr);
		logvo.setLogTime(dateNow);
		logvo.setLogInfo(logInfo);
		logvo.setLogLevel(logLevel);

		save_Hibernate_Object(logvo);
	}
}
